package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Mesto nastanka' from the chart.
 * 
 * @author Lab
 * 
 */
public class CreationPlace extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_PERIOD = "period";
    public static final String ONT_PLACE = "place";
    public static final String ONT_NAME = "name";
    public static final String ONT_CREATION = "creation";
    public static final String ONT_PRODUCTION = "production";

    public CreationPlace(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null) {
            return;
        }

        final Query query = new Query();
        final String[] result = query.select("production").type("name", CIDOC.E44.queryValue())
                .label("name", this.value).type("place", CIDOC.E53.queryValue())
                .type("production", CIDOC.E12.queryValue()).connect("place", CIDOC.P87.queryValue(), "name")
                .connect("production", CIDOC.P7.queryValue(), "place").find();

        final String itemURI = Item.formatItemURI(this.id);
        final String productionURI;
        if (result == null || result.length == 0) {
            productionURI = Ont.formatURI(this.id, ONT_PERIOD, ONT_PLACE);
            final String placeNameURI = Ont.formatURI(this.id, ONT_PLACE, ONT_NAME, ONT_CREATION);
            final String placeURI = Ont.formatURI(this.id, ONT_PLACE, ONT_CREATION);

            Ont.getOntology().createIndividual(CIDOC.E44.getFullValue(), placeNameURI);
            Ont.getOntology().createIndividual(CIDOC.E53.getFullValue(), placeURI);
            Ont.getOntology().createIndividual(CIDOC.E12.getFullValue(), productionURI);

            Ont.getOntology().addLabel(placeNameURI, this.value);

            Ont.getOntology().connectIndividuals(productionURI, CIDOC.P7.getFullValue(), placeURI);
            Ont.getOntology().connectIndividuals(placeURI, CIDOC.P87.getFullValue(), placeNameURI);

        } else {
            productionURI = result[0];
        }

        // connect to item
        Ont.getOntology().connectIndividuals(productionURI, CIDOC.P108.getFullValue(), itemURI);
    }

    public void fromRDF() {
        final String itemQueryURI = Item.formatItemQueryURI(this.id);
        final Query q = new Query();
        final String[] result = q.selectLabel().connect("production", CIDOC.P108.queryValue(), itemQueryURI)
                .type("production", CIDOC.E12.queryValue()).type("place", CIDOC.E53.queryValue())
                .connect("production", CIDOC.P7.queryValue(), "place").type("placeName", CIDOC.E44.queryValue())
                .connect("place", CIDOC.P87.queryValue(), "placeName").findLabel("placeName");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.value != null) {
            doc.add(SearchMD.CONTENT.createField(this.value));
        }
    }

}
